<?php
// +----------------------------------------------------------------------
// | ThinkBLOG (Build on ThinkPHP)
// +----------------------------------------------------------------------
// | @link ( http://www.yurnero.net )
// +----------------------------------------------------------------------
// | @licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | @author nicholas <nicholasinlove@126.com>
// +----------------------------------------------------------------------
// | $Id: MetasAction.class.php 145 2012-04-27 09:26:00Z nicholasinlove1986@gmail.com $
// +----------------------------------------------------------------------


class MetasAction extends AdminBaseAction {
	
	/**
	 * Metas 句柄
	 *
	 * @access public
	 * @var string
	 */
	public $m;
	
	/**
	 * Metas 模型句柄
	 *
	 * @access public
	 * @var string
	 */
	public $dao;
	
	/**
	 * 初始化
	 *
	 * @access public
	 * @return void
	 */
	public function _initialize() {
		
		parent::_initialize ();
		
		$this->m = new Metas ();
		$this->dao = D ( 'Metas' );
		
		if (ACTION_NAME == 'category' || ACTION_NAME == 'tag') {
			$this->m->setType ( ACTION_NAME );
		}
	}
	
	/**
	 * 分类目录 
	 *
	 * @access public
	 * @return void
	 */
	public function category() {
		
		if (! empty ( $this->r->action )) {
			
			if ($this->m->addMetas ()) {
				/** 刷新缓存 */
				$this->m->cache ();
				$this->assign ( 'success', '设置已保存。' );
			} else {
				$this->assign ( 'error', $this->m->getError () );
			}
		}
		
		$res = $this->dao->getCategoryList ( 0, 0, 0, false );
		$this->assign ( 'select', $this->dao->getCategoryList () ); // 无限分类
		$this->assign ( 'category', $res );
		$this->assign ( 'default_category', $this->dao->where ( 'metaid=1' )->getField ( 'name' ) );
		$this->assign ( 'total', count ( $res ) );
		$this->assign ( 'ur_here', array ('text' => '分类目录', 'action' => 'edit' ) );
		cookie ( 'currentUrl_', __SELF__ );
		$this->display ( 'category' );
	}
	
	/**
	 * 文章标签 
	 *
	 * @access public
	 * @return void
	 */
	public function tag() {
		
		$data = $this->r->from ( 'action', 's' );
		
		if (! empty ( $data ['action'] )) {
			
			if ($this->m->addMetas ()) {
				/** 刷新缓存 */
				$this->m->cache ();
				$this->assign ( 'success', L ( '设置已保存。' ) );
			} else {
				$this->assign ( 'error', $this->m->getError () );
			}
		}
		
		//$map = $this->_search ();
		/** 查询过滤 */
		$map = array ();
		$map ['type'] = 'tag';
		
		if ($data ['s']) {
			$map ['name'] = array ('like', '%' . $data ['s'] . '%' );
		}
		
		$this->_list ( $this->dao, $map );
		$this->assign ( 'ur_here', array ('text' => L ( '文章标签' ), 'action' => 'edit' ) );
		cookie ( 'currentUrl_', __SELF__ );
		$this->display ( 'tag' );
	}
	
	/**
	 * 编辑 metas
	 *
	 * @access public
	 * @return void
	 */
	public function edit() {
		
		$data = $this->r->from ( 'type', 'metaid', 'action', 'parent' );
		$this->m->setType ( $data ['type'] );
		
		if (! empty ( $data ['action'] )) {
			
			$goon = true;
			
			/** 分类上下级判断 */
			if ($data ['type'] == 'category') {
				/** 判断上级目录是否合法 */
				$children = array_keys ( $this->dao->getCategoryList ( $data ['metaid'], 0, 0, false ) ); // 获得当前分类的所有下级分类		
				if (in_array ( $data ['parent'], $children )) {
					/** 选定的父类是当前分类或当前分类的下级类 */
					$this->assign ( 'error', '错误：所选择的父级分类不能是当前分类或者当前分类的下级分类。' );
					$goon = false;
				}
			}
			if ($goon) {
				if ($this->m->editMetas ()) {
					/** 刷新缓存 */
					$this->m->cache ();
					$this->assign ( 'success', '设置已保存。' );
				} else {
					$this->assign ( 'error', $this->m->getError () );
				}
			}
		}
		
		$vo = $this->dao->getByMetaid ( $data ['metaid'] );
		if ($data ['type'] == 'category') {
			$title = L ( '编辑分类目录' );
			$select = $this->dao->getCategoryList ( 0, $vo ['parent'], 0, true );
			$this->assign ( 'select', $select );
		} else {
			$title = L ( '文章标签' );
		}
		
		$this->assign ( 'ur_here', array ('text' => $title, 'action' => 'edit' ) );
		$this->assign ( 'vo', $vo );
		$this->display ( $data ['type'] . '_edit' );
	}
	
	/**
	 * 删除 Metas
	 *
	 * @access public
	 * @return void
	 */
	public function delete() {
		
		$data = $this->r->from ( 'type', 'metaid' );
		
		$this->m->setType ( $data ['type'] );
		
		if ($data ['type'] == 'category' && $data ['metaid'] == 1) {
			$this->category ();
		}
		
		if ($this->m->deleteMetas ( ( array ) $data ['metaid'] )) {
			/** 刷新缓存 */
			$this->m->cache ();
		}
		redirect ( U ( 'Admin/Metas/' . $data ['type'] ) );
	}
	
	/**
	 * 刷新 Metas
	 *
	 * @access public
	 * @return void
	 */
	public function refresh() {
		$arr = $this->r->filter ( 'int' )->metaid;
		($this->r->type == 'category') ? array_push ( $arr, 1 ) : NULL;
		if ($arr && is_array ( $arr )) {
			foreach ( $arr as $var ) {
				$this->m->refresh ( $var, 'post', 'publish' );
			}
			/** 刷新缓存 */
			$this->m->cache ();
		}
		redirect ( U ( 'Admin/Metas/' . $this->r->type ) );
	}
	
	/**
	 * 编辑 Metas
	 *
	 * @access public
	 * @return void
	 */
	public function ajaxEdit() {
		$data = $this->r->from ( 'metaid', 'name', 'slug', 'taxonomy' );
		$this->m->setType ( $data ['taxonomy'] );
		if ($this->m->editMetas ()) {
			$this->m->cache ();
			$map = array ();
			$map ['metaid'] = $data ['metaid'];
			$vo = $this->dao->getOneByMap ( $map );
			$this->assign ( 'vo', $vo );
			echo $this->fetch ( 'Metas:ajax_' . $data ['taxonomy'] . '_edit' );
		} else {
			echo $this->m->getError ();
		}
	}

}